package elitech.vietnam.weatherfashion.tabs.self;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dialog.PicPickerDialog;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

public class SelfFragment extends AbstractFragment implements View.OnClickListener {

	public static final String TAB_MYSTYLE = "MYSTYLE";
	public static final String TAB_MYHEART = "MYHEART";
	
	private FragmentTabHost mTabHost;
	private Button mBtnCamera;
	private TextView mTextHeader;
	private NotifyButton button_notification;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.self1, null);
		setupView(view);
		
		mTabHost = (FragmentTabHost) view.findViewById(R.id.self_tabhost);
		mBtnCamera = (Button) view.findViewById(R.id.self_button_camera);
		mTextHeader = (TextView) view.findViewById(R.id.self_text_header);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);
		
		mTextHeader.setText(Html.fromHtml(getString(R.string.mystyle)));
		mBtnCamera.setOnClickListener(this);
		mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.self_realtabcontent);

		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		Bundle bundle = new Bundle();
		bundle.putString("page", TAB_MYSTYLE);
		mTabHost.addTab(mTabHost.newTabSpec(TAB_MYSTYLE).setIndicator(createTabView(getActivity(), R.drawable.mystyle_d, R.drawable.mystyle_s, getString(R.string.mystyletab))),
				SelfChildFragment.class, bundle);
		
		bundle = new Bundle();
		bundle.putString("page", TAB_MYHEART);
		mTabHost.addTab(mTabHost.newTabSpec(TAB_MYHEART).setIndicator(createTabView(getActivity(), R.drawable.myheart_d, R.drawable.myheart_s, getString(R.string.myheart))),
				SelfChildFragment.class, bundle);
		
		return view;
	}

	private View createTabView(final Context context, int selected, int normal, String title) {
		View view = LayoutInflater.from(context).inflate(R.layout.styler_tab, null);
		ImageView item_image = (ImageView) view.findViewById(R.id.styler_childtab_itemimage);
		TextView item_title = (TextView) view.findViewById(R.id.styler_childtab_itemtext);
		Drawable drawable = Utils.getTabDrawable(context, selected, normal);
		item_image.setImageDrawable(drawable);
		item_title.setText(title);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.self_button_camera:
			new PicPickerDialog(getActivity(), this).show();
			break;

		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);
			break;

		default:
			break;
		}
	}
}
